package com.zhoug.bluetooth;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.ParcelUuid;
import android.provider.Settings;


import com.zhoug.logging.Logger;

import java.io.Closeable;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

/**
 * @Author: zhoug
 * @Date: 2024-06-25
 * @Description:
 */
@SuppressLint("MissingPermission")
public class BluetoothUtil {
    private static final String TAG = ">>>zbluetooth";

    /**
     * 有效的蓝牙设备
     *
     * @param device
     * @return
     */
    public static boolean isEffectiveDevice(BluetoothDevice device) {
        return device != null && device.getName() != null && device.getAddress() != null;
    }

    /**
     * 获取蓝牙配对状态名称
     *
     * @param bondState {@link BluetoothDevice#getBondState()}
     * @return
     */
    public static String getBondStateName(int bondState) {
        if (bondState == BluetoothDevice.BOND_NONE) {
            return "未配对";
        } else if (bondState == BluetoothDevice.BOND_BONDING) {
            return "正在配对";
        } else if (bondState == BluetoothDevice.BOND_BONDED) {
            return "已配对";
        }
        return "";
    }

    /**
     * 按配对状态排序
     *
     * @param devices
     */
    public static void sortByBondState(List<BluetoothDevice> devices) {
        if (devices != null && !devices.isEmpty()) {
            Collections.sort(devices, (device1, device2) -> device2.getBondState() - device1.getBondState());
        }
    }

    /**
     * 判断gps是否打开
     *
     * @param context
     * @return
     */
    public static boolean isGpsOpened(Context context) {
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        // 通过GPS卫星定位，定位级别可以精确到街（通过24颗卫星定位，在室外和空旷的地方定位准确、速度快）
        return locationManager != null && locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }

    public static void openGps(Context context) {
        Intent gpsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        context.startActivity(gpsIntent);
    }


    public static void closeIO(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 判断是否是打印机
     *
     * @param device
     * @return
     */
    public static boolean isPrinter(BluetoothDevice device) {
        return BluetoothClass.Device.Major.IMAGING == getDeviceType(device);
    }

    /**
     * 获取设备类型
     *
     * @param device
     * @return
     */
    public static int getDeviceType(BluetoothDevice device) {
        if (device == null) return 0;
        int majorDeviceClass = 0;
        BluetoothClass bluetoothClass = device.getBluetoothClass();
        if (bluetoothClass != null) {
            majorDeviceClass = bluetoothClass.getMajorDeviceClass();
        }
        //1536 600
//        ZLoger.d(TAG, device.getName() + ":majorDeviceClass=" + majorDeviceClass);
        return majorDeviceClass;
    }


    public static void printUUID(BluetoothDevice device) {
        if (device != null) {
            String name = device.getName();
            if (name == null) {
                name = "未命名设备";
            }
            Logger.d(TAG, "##############" + name + "###########");
            ParcelUuid[] uuids = device.getUuids();
            if (uuids != null && uuids.length > 0) {
                for (ParcelUuid item : uuids) {
                    if (item != null && item.getUuid() != null) {
                        Logger.d(TAG, "uuid:" + item.getUuid());
                    }
                }
            }

        }

    }

}
